/*
 * #depends web-worker-folder-store.js
 */
Ext.ns('Ext.ux.mattgoldspink.subsonic');

Ext.ux.mattgoldspink.subsonic.TrackGrid = Ext.extend(Ext.grid.GridPanel, {
    constructor: function(config) {
        Ext.apply(this, config, {
			store: Ext.ux.mattgoldspink.subsonic.FolderTracksStore,
			columns: [
				{header: ' ', width: 24, sortable: false, dataIndex: 'playstate', menuDisabled: true, hideable: false, renderer: function(value) {
					if (value === 'playing') {
						return '<img src="./icons/current-track-playing-icon.png" />';
					} else if (value === 'paused') {
						return '<img src="./icons/current-track-paused-icon.png" />';
					} else {
						return '';
					}
				}}, 
				{header: 'No.', width: 28, sortable: true, dataIndex: 'track', hidden: true},
				{header: 'Name', width: 225, sortable: true, dataIndex: 'title'},
				{header: 'Time', width: 75, sortable: true, dataIndex: 'duration', renderer: Ext.ux.mattgoldspink.subsonic.Util.renderTime},
				{header: 'Artist', width: 225, sortable: true, dataIndex: 'artist'},
				{header: 'Album', width: 225, sortable: true,  dataIndex: 'album'},
				{header: 'Genre', width: 225, sortable: true,  dataIndex: 'genre'},
				{header: 'Year', width: 85, sortable: true,  dataIndex: 'year'}
			],
			stripeRows: true,
			cls: 'tracklist-grid',
			trackMouseOver: false,
			deferRowRender: true,
			enableDrag: true,
			ddGroup: 'grid2tree',
			view: new Ext.ux.grid.BufferView({
				// render rows as they come into viewable area.
				scrollDelay: 10,
				rowHeight: 15,
				borderHeight: 0
			}),
			//sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			//loadMask: true,
			plugins: ['msgbus'],
			listeners: {
				scope: this,
				rowdblclick: function(grid, rowIndex, e) {
					var record = grid.getSelectionModel().getSelected();
					if (record.get('isDir') === 'true') {
						this.publish('subsonic.folder.click', record.data);
					} else {
						this.publish('subsonic.player.play.playlist', {
							tracks: this.getStore(),
							startAtTrack: rowIndex   
						});
					}
				},
				activate: function() {
					this.view.refresh();
				}
			}
		});
        Ext.ux.mattgoldspink.subsonic.TrackGrid.superclass.constructor.apply(this, arguments);
        this.setupSubscriptions();
    },
    setupSubscriptions: function(){
        this.subscribe('subsonic.track.nowplaying', {fn: function(subject, data) {
            var record = this.store.getById(data.id);
            if (record) {
                record.beginEdit();
                record.set('playstate', 'playing');
                record.endEdit();
            }
        }, scope: this});
        this.subscribe('subsonic.track.paused', {fn: function(subject, data) {
            var record = this.store.getById(data.id);
            if (record) {
                record.beginEdit();
                record.set('playstate', 'paused');
                record.endEdit();
            }
        }, scope: this});
        this.subscribe('subsonic.track.stopped', {fn: function(subject, data) {
            var record = this.store.getById(data.id);
            if (record) {
                record.beginEdit();
                record.set('playstate', '');
                record.endEdit();
            }
        }, scope: this});
		this.subscribe('subsonic.grid.jumpto', {
			scope: this,
			fn: this.handleGridJumpTo
		});
    } ,
	handleGridJumpTo: function(topic, item) {
		var id = item.id;
		var index = this.getStore().indexOfId(id);
		if (index > -1) {
			this.getView().focusRow(index);
			this.getSelectionModel().selectRow(index);
		}
	}
});
